﻿
//  Copyright (c) Microsoft Corporation.  All Rights Reserved.

using System;
using System.ServiceModel;
using System.IO;
using DsLib;

namespace Microsoft.Samples.GettingStarted
{
    //The service contract is defined in generatedClient.cs, generated from the service by the svcutil tool.

    //Client implementation code.
    class Client
    {
        static void Main()
        {
            HelloWorld();
            Calculator();
            StartBSTTest();
        }

        static void StartBSTTest()
        {
            TestStringBST2();

            Console.ReadKey();
        }


        #region BSTTests

        public static void TestStringBST2()
        {
            string filePath = @"..\eng_words.txt";
            if (File.Exists(filePath))
            {
                string[] arr = File.ReadAllLines(filePath);
                BSTServiceOf_StringClient client = new BSTServiceOf_StringClient();

                // Randomize the input
                Random rand = new Random();
                for (int i = 0; i < arr.Length; i++)
                {
                    int randNum = rand.Next(0, arr.Length - 1);

                    string s = arr[i];
                    arr[i] = arr[randNum];
                    arr[randNum] = s;
                }

                // Add to tree
                foreach (string s in arr)
                {
                    client.Add(s);
                }

                // Do Search
                client.PrintValueStatusInBST("munish");
                client.PrintValueStatusInBST("zoom");
                client.PrintValueStatusInBST("mute");
                client.PrintValueStatusInBST("rouse");
                client.PrintValueStatusInBST("zulusi");

                // Do traversals
                string traversal;
                Console.WriteLine("Inorder: ");
                client.PrintTraversePath(TreeTraversalType.InOrder);

                Console.WriteLine("Preorder: ");
                traversal = client.GetTraversePath(TreeTraversalType.PreOrder);

                Console.WriteLine("Postorder: ");
                traversal = client.GetTraversePath(TreeTraversalType.PostOrder);
            }
        }

        #endregion
        

        static void HelloWorld()
        {
            HelloWorldServiceClient hwc = new HelloWorldServiceClient();
            int i = 0;
            while(i++ < 10)
            {
                string result = hwc.SayHello("MG");
                Console.WriteLine(result);
            }

            hwc.Close();
            Console.ReadKey();
        }

        static void Calculator()
        {
            // Create a client
            CalculatorClient client = new CalculatorClient();

            // Call the Add service operation.
            double value1 = 100.00D;
            double value2 = 15.99D;
            double result = client.Add(value1, value2);
            Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

            // Call the Subtract service operation.
            value1 = 145.00D;
            value2 = 76.54D;
            result = client.Subtract(value1, value2);
            Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);

            // Call the Multiply service operation.
            value1 = 9.00D;
            value2 = 81.25D;
            result = client.Multiply(value1, value2);
            Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);

            // Call the Divide service operation.
            value1 = 22.00D;
            value2 = 7.00D;
            result = client.Divide(value1, value2);
            Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);

            //Closing the client gracefully closes the connection and cleans up resources
            client.Close();

            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to terminate client.");
            Console.ReadLine();
        }
    }
}
